<template>
    <div>
        <el-table v-loading="loading" :data="userList">
            <el-table-column label="商户名称" align="center" prop="commercialTenantName" />
            <el-table-column label="商户编号" align="center" prop="commercialTenantNo"/>
            <el-table-column label="商户手机号" align="center" prop="commercialTenantPhone" />
            <el-table-column label="登录账号" align="center" prop="login_code" />
            <el-table-column label="邮件" align="center" prop="commercialTenantEmil" />
            <el-table-column label="最后登录IP" align="center" prop="loginIp"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
                <template #default="scope">
                    <el-button type="text" icon="Key" @click="handleResetPwd(scope.row)"
                               v-hasPermi="['tenant:user:changePassword']">重置密码
                    </el-button>
                    
                    <el-button v-if="scope.row.login_code == null" type="text" icon="Key" @click="createLogin(scope.row)"
                               v-hasPermi="['tenant:user:changePassword']">创建登录账号
                    </el-button>

                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script setup>
import {defineProps, getCurrentInstance, ref} from 'vue';
import {listUser, resetPwd,logincode} from '@/api/tenant/user';
import {useRoute} from 'vue-router';

const loading = ref(true);
const userList = ref([]);
const route = useRoute();
const total = ref(0);
console.log(route);
const {proxy} = getCurrentInstance();
const {commercial_tenant_state} = proxy.useDict('commercial_tenant_state');
const props = defineProps({
    outMerchantNo: {
        type: String,
        default: '',
    },
});
getList()

// 查询商户列表
function getList() {
    loading.value = true;
    const params = {
        outmerchantno: props.outMerchantNo
    }
    listUser(params).then((response) => {
        console.log(response)
        userList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

/** 重置密码按钮操作 */
function handleResetPwd(row) {
    proxy
        .$prompt('请输入"' + row.commercialTenantName + '"的新密码', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            closeOnClickModal: false,
            inputPattern: /^.{5,20}$/,
            inputErrorMessage: '用户密码长度必须介于 5 和 20 之间',
        })
        .then(({value}) => {
            resetPwd(row.id, value).then(() => {
                proxy.$modal.msgSuccess('修改成功，新密码是：' + value);
            });
        })
        .catch(() => {
        });
}

function createLogin(row) {
    logincode(row.id).then(()=>{
        proxy.$modal.msgSuccess("账号生成成功");
        getList();
    })
}
</script>

<style></style>
